package com.pinyougou.service;

import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.SellerService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * @auther lmj
 * @date 2019/10/28
 * 认证类
 */
public class UserDetailsServiceImpl implements UserDetailsService {

    private SellerService sellerService;

    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println("经过了UserDetailsServiceImpl实现类了");
        // 创建角色
        List<GrantedAuthority> grantedAuthorityArrayList = new ArrayList<GrantedAuthority>();
        grantedAuthorityArrayList.add(new SimpleGrantedAuthority("ROLE_SELLER"));
        // 得到商家对象
        TbSeller seller = sellerService.findOne(username);
        if (seller != null) {
            // 判断当前用户是否被审核通过
            if ("1".equals(seller.getStatus())) {
                return new User(username, seller.getPassword(), grantedAuthorityArrayList);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}
